<?php

//Open db

$db = new Database();
$db->connectToDB();


echo '<div class="flightBoxHeader">
		<div class="headerheenreis">
			<span class="headerheen">Heenreis</span><br/>
			<span class="headerheendatum"> <a href=' . $_SESSION['search']->getDay(1, 0) . '>< </a> ' . $_SESSION['search']->getDepartureDate() . '  <a href=' . $_SESSION['search']->getDay(1, 1) . '><span class="orangeArrow">></span></a> </span>
		</div>
		';
if ($_SESSION['search']->getReturnDate() != "") {
    echo '
			<div class="seperator"></div>
			<div class="headerterugreis">
				<span class="headerterug">Terugreis</span><br/>
				<span class="headerterugdatum"> <a href=' . $_SESSION['search']->getDay(0, 0) . '>< </a>  ' . $_SESSION['search']->getReturnDate() . '  <a href=' . $_SESSION['search']->getDay(0, 1) . '><span class="orangeArrow">></span></a></span>
			</div>';
}
echo '	
		<div class="seperator"></div>
		<div class="prijsPerVolwassene">
			<span class="prijsPerVol"><br/>Prijs per volwassene</span>
		</div>
</div>';

foreach ($results as $ticket) {

    //Kijken of aan alle filter voldaan word
    //if(count(array_unique($ticket->filter)) === 1)  {
    print_r($ticket->flightNumber);
    echo'<div class="flightBox">
			<div class="flightBoxLeftOperator">
				<div class="carrierLogo">';
    $carrier = $db->getCarrierInfo($ticket->operatingCarrier);
    echo '<img class="carLogo" src="images/logos/' . $carrier[1] . '" alt="' . $carrier[0] . ' logo"/>';
    echo '</div>';

    echo '
				<div class="flightDetails">
					<a  id="flight-' . $ticket->idTicket . '" onclick="showPopup(' . $ticket->idTicket . ')">
						
						<img class="flightDetailsImg" src="images/details.png" alt="details" />Vlucht details
					</a>
				</div>
			</div>';
                             echo '<div class="flightBoxLeftImage"></div>';
			echo '<div class="flightBoxFlightInfo">
				<div class="heenreis">
					<div class="heengegevens">
					';

    $fromTime = $ticket->flights_outbound[0]['fromDateTime']->format("h:i");
    $dp = count($ticket->flights_outbound) - 1;
    $toTime = $ticket->flights_outbound[$dp]['toDateTime']->format("h:i");

    echo '
						<div class="timeAirport"><b>' . $fromTime . '</b> ' . $ticket->flights_outbound[0]['fromIATA'] . ' - <b> ' . $toTime . ' </b> ' . $ticket->flights_outbound[$dp]['toIATA'] . ' </div>
						<div class="heenStops"> ' . $dp . ' Tussenstop: <span class="whereStop">';

    for ($x = 1; $x <= $dp; $x++) {
        if ($x == $dp) {
            echo $ticket->flights_outbound[$x]['fromIATA'];
        } else {
            echo $ticket->flights_outbound[$x]['fromIATA'] . ',';
        }
    }

    echo
    '</span></div>
						<div class="travelTime"> ' . $ticket->journeyTime . '</div>
					</div>
				</div>
					';

    if ($_SESSION['search']->getRetour()) {

        $fromTime = $ticket->flights_inbound[0]['fromDateTime']->format("h:i");
        $dp = count($ticket->flights_inbound) - 1;
        $toTime = $ticket->flights_inbound[$dp]['toDateTime']->format("h:i");

        echo '
				<div class="flightInfoSeperator"></div>
				<div class="heenreis">
					<div class="teruggegevens">
						<div class="timeAirport"><b>' . $fromTime . '</b> ' . $ticket->flights_inbound[0]['fromIATA'] . ' - <b> ' . $toTime . ' </b> ' . $ticket->flights_inbound[$dp]['toIATA'] . '</div>
						<div class="heenStops"> ' . $dp . ' Tussenstop: <span class="whereStop">';
        //$totalTime = $ticket->flights_inbound[0]['fromDateTime']->format("h:i");


        for ($x = 1; $x <= $dp; $x++) {
            if ($x == $dp) {
                echo $ticket->flights_inbound[$x]['fromIATA'];
            } else {
                echo $ticket->flights_inbound[$x]['fromIATA'] . ',';
            }
        }

        echo
        '</span></div>
						<div class="travelTime"> ' . $ticket->journeyTime . '</div>
					</div>
				</div>
					';
    }

    echo '
				<div class="flightInfoSeperator"></div>
				<div class="book">
					<div class="bookingPrice">&euro; ' . $ticket->totalPrice . '</div>
					<a href="' . $ticket->url . '"><button width="20"  name="submit" value="" class="bookingButton">Boeken <span style=" float:right;">></span></button></a>
				</div>
				<div class="flightBoxFooterSeperator"></div>
				<div class="flightBoxFooter">
					<!--<div class="flightBoxFooterInfo">Ook beschikbaar bij: <a href="#" class="alsoAvaible" >Transavia &euro; 3444</a>   <a href="#" class="alsoAvaible">Transavia &euro; 3444</a>   <a href="#" class="alsoAvaible">Transavia &euro; 3444</a> </div>-->
				</div>
			</div>
		</div>';
}

echo '
        <div id="pagination">
                <div class="paginationLinks">
                        <div class="pagina">Pagina</div>';

$pagination = new Pagination();
if (!isset($_SESSION['resulthandler'])) {
    $result = $pagination->calculate_pages($resultHandler->getTotalRows(), $resultHandler->getTotalRowsOnPage(), 0);
} else {
    $result = $pagination->calculate_pages($_SESSION['resulthandler']->getTotalRows(), $_SESSION['resulthandler']->getTotalRowsOnPage(), 0);
}
foreach ($result['pages'] as $page) {
    if ($page == 1) {
        echo "<a class=\"pagination\" id=" . $page . "  style=\"background-color:#f25900; color:#FFF;\" onclick=\"nextPage('results', $page)\">" . $page . "</a>";
    } else {
        echo "<a class=\"pagination\" id=" . $page . "  style=\"color:#FFF;\" onclick=\"nextPage('results', $page)\">" . $page++ . "</a>";
    }
}
echo '
                  </div>
        </div> ';